package com.umakr.ax.app.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.umakr.ax.app.vo.AutoMenuSaveVO;
import com.umakr.ax.common.mapper.AutoMenuMapper;
import com.umakr.ax.common.model.AutoMenu;
import com.umakr.ax.common.service.AutoMenuService;
import com.umakr.ax.core.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 微信公众号异常消息 服务实现类
 * </p>
 *
 * @author Wmdz
 * @since 2018-01-09
 */
@Service
public class AutoMenuWebService extends BaseService<AutoMenuMapper, AutoMenu>{

    @Autowired
    AutoMenuService autoMenuService;

    /**
     * 获取电台的自定义菜单
     * @param param
     * @return
     */
    public Object detail(@RequestParam Map<String, Object> param) {
        return resultMap(autoMenuService.detail(param));
    }

    public Object saveOrUpdateAutoMenu(AutoMenuSaveVO vo){
        Date date = new Date();

        if(vo.getMenuButton() != null) {
            EntityWrapper<AutoMenu> autoEn = new EntityWrapper<>();
            autoEn.eq("rs_id", vo.getRsId());
             AutoMenu autoMenu = selectOne(autoEn);
            if (autoMenu == null) {
                autoMenu = new AutoMenu();
                autoMenu.setCreateTime(date);
            }
            autoMenu.setUpdateTime(date);
            autoMenu.setRsId(vo.getRsId());
            autoMenu.setIsdeleted(0);
            autoMenu.setStatus(1);

            JSONObject menuJsonObject = new JSONObject();
            JSONArray menuJsonArray = new JSONArray();
            for (int i = 0; i < vo.getMenuButton().size(); i++) {
                Map<String, Object> menuBtn = vo.getMenuButton().get(i);
                menuBtn.put("subMenuVisible", "true".equals(menuBtn.get("subMenuVisible")));
                if(vo.getSubButton() != null ){
                    if(i < vo.getSubButton().size()){
                        menuBtn.put("sub_menu", vo.getSubButton().get(i));
                    }else {
                        menuBtn.put("sub_menu", "[]");
                    }
                }
                menuJsonArray.add(menuBtn);
            }
            menuJsonObject.put("button", menuJsonArray);
            autoMenu.setData(menuJsonObject.toJSONString());

            update(autoMenu);
            return resultMap(1,"保存成功");
        }else{
            return resultMap(0,"至少增加一个主菜单");
        }
    }

    public Object getAutoMenuDetail(Long rsId) {
        EntityWrapper<AutoMenu> autoEn = new EntityWrapper<>();
        autoEn.eq("rs_id",rsId);
        AutoMenu autoMenu = selectOne(autoEn);

        List<Map<String,Object>> menuButton = Lists.newArrayList();
        List<List<Map<String,Object>>> subButton = Lists.newArrayList();

        AutoMenuSaveVO vo = new AutoMenuSaveVO();
        vo.setRsId(rsId);

        if(autoMenu != null && autoMenu.getData() != null) {
            JSONObject menuData = JSONObject.parseObject(autoMenu.getData());
            JSONArray btnArray = JSONArray.parseArray(menuData.get("button").toString());
            for (int i = 0; i < btnArray.size(); i++) {
                JSONObject menuBtn = JSONObject.parseObject(btnArray.get(i).toString());
                if(menuBtn.get("sub_menu") == null || "".equals(menuBtn.get("sub_menu").toString())){
                    menuBtn.remove("sub_menu");
                    menuButton.add(menuBtn);
                }else {
                    JSONArray subItems = JSONArray.parseArray(menuBtn.get("sub_menu").toString());
                    menuBtn.remove("sub_menu");
                    menuButton.add(menuBtn);

                    List<Map<String, Object>> subButtonItems = Lists.newArrayList();

                    for (Object item : subItems) {
                        subButtonItems.add((Map<String, Object>) item);
                    }
                    subButton.add(subButtonItems);
                }
            }
        }

        vo.setMenuButton(menuButton);
        vo.setSubButton(subButton);

        return resultMap(vo);
    }

}
